/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			utils.h
  \date			September 2013
  \author		TNick
    
*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef LIBALC_utils_h_INCLUDE
#define LIBALC_utils_h_INCLUDE
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include <libalc/libalc_global.h>
#include <libalc/data_types.h>
#include <libalc/error_codes.h>

#if !defined _WIN32_WCE
#include <errno.h>
#endif
#include <stddef.h>
#include <stdio.h>
#if defined _WIN32
#include <winsock2.h>
#endif

#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#ifdef __cplusplus
extern "C" {
#endif

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

//!  Provide random number from 0..(num-1)
/// from zhelpers.h
#if (defined (__WINDOWS__))
#   define randof(num)  (int) ((float) (num) * rand () / (RAND_MAX + 1.0))
#else
#   define randof(num)  (int) ((float) (num) * random () / (RAND_MAX + 1.0))
#endif


// ANSI color codes
#ifdef TERMINAL_WITH_COLORS
#    define CCOD_RS "\x1B[0m"    // reset
#    define CCOD_HC "\x1B[1m"    // hicolor
#    define CCOD_UL "\x1B[4m"    // underline
#    define CCOD_INV "\x1B[7m"   // inverse background and foreground
#    define CCOD_FBLK "\x1B[30m" // foreground black
#    define CCOD_FRED "\x1B[31m" // foreground red
#    define CCOD_FGRN "\x1B[32m" // foreground green
#    define CCOD_FYEL "\x1B[33m" // foreground yellow
#    define CCOD_FBLE "\x1B[34m" // foreground blue
#    define CCOD_FMAG "\x1B[35m" // foreground magenta
#    define CCOD_FCYN "\x1B[36m" // foreground cyan
#    define CCOD_FWHT "\x1B[37m" // foreground white
#    define CCOD_BBLK "\x1B[40m" // background black
#    define CCOD_BRED "\x1B[41m" // background red
#    define CCOD_BGRN "\x1B[42m" // background green
#    define CCOD_BYEL "\x1B[43m" // background yellow
#    define CCOD_BBLE "\x1B[44m" // background blue
#    define CCOD_BMAG "\x1B[45m" // background magenta
#    define CCOD_BCYN "\x1B[46m" // background cyan
#    define CCOD_BWHT "\x1B[47m" // background white
#else // TERMINAL_WITH_COLORS
#    define CCOD_RS     // reset
#    define CCOD_HC     // hicolor
#    define CCOD_UL     // underline
#    define CCOD_INV    // inverse background and foreground
#    define CCOD_FBLK  // foreground black
#    define CCOD_FRED  // foreground red
#    define CCOD_FGRN  // foreground green
#    define CCOD_FYEL  // foreground yellow
#    define CCOD_FBLE  // foreground blue
#    define CCOD_FMAG  // foreground magenta
#    define CCOD_FCYN  // foreground cyan
#    define CCOD_FWHT  // foreground white
#    define CCOD_BBLK  // background black
#    define CCOD_BRED  // background red
#    define CCOD_BGRN  // background green
#    define CCOD_BYEL  // background yellow
#    define CCOD_BBLE  // background blue
#    define CCOD_BMAG  // background magenta
#    define CCOD_BCYN  // background cyan
#    define CCOD_BWHT  // background white
#endif // TERMINAL_WITH_COLORS

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

//!  Sleep for a number of milliseconds
/// from zhelpers.h
LIBALC_EXPORT void
z_sleep (int msecs);

//!  Return current system clock as milliseconds
/// from zhelpers.h
LIBALC_EXPORT int64_t
z_clock (void);

//!  Print formatted string to stdout, prefixed by date/time and
//!  terminated with a newline.
/// from zhelpers.h
LIBALC_EXPORT void
log_message (const char *format, ...);

//!  Print formatted string to stdout, prefixed by date/time and
//!  terminated with a newline.
/// from zhelpers.h
LIBALC_EXPORT void
err_message (const char *format, ...);

#ifdef	LIBALC_DEBUG
//!  Print formatted string to stdout, prefixed by date/time and
//!  terminated with a newline.
/// from zhelpers.h
LIBALC_EXPORT void
dbg_message (const char *format, ...);
#else
static inline void
dbg_message (const char *format, ...) {
	do { (void)(format); } while (0);
}
#endif

//! output the name of the function
#define dbg_func dbg_message (__func__)

//! output the name of the function and the string "fails"
#define dbg_func_fail(err) dbg_message ("%s FAILS: %d!", __func__, err)

//! output the name of the function and the string "fails" and return the error code
#define return_dbg_func_fail(err) \
{ \
    dbg_message ("%s FAILS (%d)!", __func__, err); \
    return err; \
}

//! get the 64-bit signed number from a string
LIBALC_EXPORT func_error_t
alc_string_to_i64 (const char *str, ALC64S * out_int);

//! get the 64-bit unsigned number from a string
LIBALC_EXPORT func_error_t
alc_string_to_u64 (const char *str, ALC64U * out_int);


void alc_sleep_seconds ( long seconds );

void alc_sleep_ticks ( long ticks );

void alc_wait(long milliseconds);


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifdef __cplusplus
}
#endif
#endif // LIBALC_utils_h_INCLUDE
